package com.lxr0860.lxr0860production_test.itemactivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.ToastUtils;
import com.sdk.devicemanager.DEVICEcon;

import org.jetbrains.annotations.Nullable;

/**
 * Created by uniriho on 2018/6/5.
 */

public class SystemActivity extends Activity implements View.OnClickListener {
    private TextView mTvTips;
    private Button mBtnEnableApk;
    private Button mBtnDisableApk;
    private Button mBtnRestAdbPassword;
    private Button mBtnRestSettingPassword;
    private Button mBtnoPenSettingPW;
    private Button mBtnCloseSettingPW;
    private EditText mEtOldPassword, mEtNewPassword;
    private Button mBtnChangeAdbPassword, mBtnChangeSettingPassword;
    private DEVICEcon mDevicecon;
    private String oldPassword, newPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_system);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mTvTips = (TextView) findViewById(R.id.tv_system_tip);
        mBtnEnableApk = (Button) findViewById(R.id.btn_system_enable_apk);
        mBtnDisableApk = (Button) findViewById(R.id.btn_system_disable_apk);
        mBtnRestAdbPassword = (Button) findViewById(R.id.btn_system_reset_adb_password);
        mBtnRestSettingPassword = (Button) findViewById(R.id.btn_system_reset_setting_password);
        mEtOldPassword = (EditText) findViewById(R.id.et_system_old_passwrod);
        mEtNewPassword = (EditText) findViewById(R.id.et_system_new_passwrod);
        mBtnChangeAdbPassword = (Button) findViewById(R.id.btn_system_change_adb_password);
        mBtnChangeSettingPassword = (Button) findViewById(R.id.btn_system_change_setting_password);
        mBtnoPenSettingPW = (Button) findViewById(R.id.btn_system_open_setting_password);
        mBtnCloseSettingPW = (Button) findViewById(R.id.btn_system_close_setting_password);
    }

    private void initData() {
        mDevicecon = new DEVICEcon(this);
    }

    private void initEvent() {
        mBtnEnableApk.setOnClickListener(this);
        mBtnDisableApk.setOnClickListener(this);
        mBtnRestAdbPassword.setOnClickListener(this);
        mBtnRestSettingPassword.setOnClickListener(this);
        mBtnChangeAdbPassword.setOnClickListener(this);
        mBtnChangeSettingPassword.setOnClickListener(this);
        mBtnCloseSettingPW.setOnClickListener(this);
        mBtnoPenSettingPW.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_system_enable_apk) {
            if (mDevicecon.setAppInstallEnabled(1)) {
                mTvTips.setText("允许APK安装成功");
            } else {
                mTvTips.setText("允许APK安装失败");
            }
        } else if (v.getId() == R.id.btn_system_disable_apk) {
            if (mDevicecon.setAppInstallEnabled(0)) {
                mTvTips.setText("限制APK安装成功");
            } else {
                mTvTips.setText("限制APK安装失败");
            }
        } else if (v.getId() == R.id.btn_system_reset_setting_password) {

            if (resetSettingPassword(SystemActivity.this)) {
                mTvTips.setText("重置设置验证密码成功，初始密码12345678");
            } else {
                mTvTips.setText("重置设置验证密码失败");
            }

        } else if (v.getId() == R.id.btn_system_change_adb_password) {
            ToastUtils.showMessage("暂不支持");
/*oldPassword = mEtOldPassword.getText().toString();
                newPassword = mEtNewPassword.getText().toString();
                if (oldPassword != null && oldPassword.trim().length() > 0) {
                    if (newPassword != null && newPassword.trim().length() > 0) {
                        boolean changeAdbPassword = mDevicecon
                                .setAdbPassword(oldPassword, newPassword);
                        if (changeAdbPassword) {
                            mTvTips.setText("修改ADB密码成功");
                        } else {
                            mTvTips.setText("修改ADB密码失败");
                        }
                    } else {
                        mTvTips.setText("新密码不能为空");
                    }
                } else {
                    mTvTips.setText("原始密码不能为空");
                }
                breck;*/
        } else if (v.getId() == R.id.btn_system_reset_adb_password) {
            ToastUtils.showMessage("暂不支持");
               /* if (mDevicecon.resetAdbPassword()) {
                    mTvTips.setText("重置ADB密码成功，初始密码12345678");
                } else {
                    mTvTips.setText("重置ADB密码失败");
                }*/
        } else if (v.getId() == R.id.btn_system_change_setting_password) {

            oldPassword = mEtOldPassword.getText().toString();
            newPassword = mEtNewPassword.getText().toString();

            if (oldPassword != null && oldPassword.trim().length() > 0) {
                if (newPassword != null && newPassword.trim().length() > 0) {
                    boolean changeSettingPassword = setSettingsPassword(SystemActivity.this, oldPassword, newPassword);
                    if (changeSettingPassword) {
                        mTvTips.setText("修改设置验证密码成功");
                    } else {
                        mTvTips.setText("修改设置验证密码失败");
                    }
                } else {
                    mTvTips.setText("新密码不能为空");
                }
            } else {
                mTvTips.setText("原始密码不能为空");
            }
        } else if (v.getId() == R.id.btn_system_open_setting_password) {
            /*if (mDevicecon.SettingsPasswordEnable(1)) {
                    mTvTips.setText("设置验证密码开启成功");
                } else {
                    mTvTips.setText("设置验证密码开启失败");
                }*/
            ToastUtils.showMessage("暂不支持");
        } else if (v.getId() == R.id.btn_system_close_setting_password) {
            /*if (mDevicecon.SettingsPasswordEnable(0)) {
                    mTvTips.setText("设置验证密码关闭成功");
                } else {
                    mTvTips.setText("设置验证密码关闭失败");
                }*/
            ToastUtils.showMessage("暂不支持");
        }
    }

    /**
     * 重置设置密码
     *
     * @return
     */
    public boolean resetSettingPassword(Context context) {

        return Settings.System.putString(context.getContentResolver(), "settings_switch_on_passwd", "12345678");

    }

    /**
     * 修改设置密码
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    public boolean setSettingsPassword(Context context, String oldPassword, String newPassword) {

        String pw = Settings.System.getString(context.getContentResolver(), "settings_switch_on_passwd");
        if (!pw.equals(oldPassword)) {
            Log.d("devicemanager", "setSettingsPassword : setting  password error.");
            return false;
        } else {
            return Settings.System.putString(context.getContentResolver(), "settings_switch_on_passwd", newPassword);
        }
    }
}
